﻿import com.Mxml;
class com.Player
{
	private var loadW:Number=175;//加载条的长度;由于它是随时间变化的.所以这边用常量先定义一下;
	private var setTimeW:Number=17;//没考虑到的;
	private var Svolue:Number=100;//保存当前音量;
	private var _mc : MovieClip;
	private var MusicArray : Array;
	//存放所有已播的声音;
	private var i : Number;
	static var PLA:Player;
	//存放当前的音乐;
	private var myxml : Mxml;
	private var isLoaded:Boolean
	private var isDrag:Boolean;
	//private var Shared:SharedObject;
	private var setLoaded:Number;
	function Player (mc : MovieClip)
	{
		this._mc = mc;
		i = 0;
		myxml = new Mxml ();
		//Shared= SharedObject.getLocal("crl");
	}
	static function instance(mc:MovieClip){
		if(PLA==null){
			PLA=new Player(mc)
		}
		return PLA
	}
	public function loadXML (xml : String)
	{
		myxml.ignoreWhite=true;
		myxml.load (xml);
		myxml.addEventListener ("init", this);
	}
	private function init ()
	{
		//trace(myxml)
		var _this=this;
		MusicArray = new Array (1, 2);
		//if (Shared.data.Duration == null) {
			_this.loadMusic (0);
		/*}else{
			trace("doShared")
			loadMusic(Shared.data.ID)
			var t:Number = Number(Shared.data.Duration);
		//读取存上次时间
			_this.setLoaded=setInterval(_this,"played", 1000, t);
		}*/
		initBtn ();
	}
	private function played(T:Number){
		_mc["MD" + i].play(T);
		clearInterval(setLoaded);
		trace("clear"+T+_mc["MD" + i]);
	}
	private function initBtn ()
	{
		var _this = this;
		_mc.Prev.onPress = function ()
		{
			_this.loadMusic ( - 1);
		}
		_mc.Next.onPress = function ()
		{
			_this.loadMusic (1);
		}
		//下面为按钮事件;
		_mc.Pause.onPress = function ()
		{
			//暂停按钮事件;
			if (_this._mc["MD" + _this.i].playing)
			{
				_this._mc["MD" + _this.i].pause ();
				_this._mc.mix.gotoAndPlay (2);
				this.gotoAndStop (2);
			} else 
			{
				_this._mc.Stop.enabled = true;
				_this._mc["MD" + _this.i].play ();
				_this._mc.mix.gotoAndStop (1);
				this.gotoAndStop (1);
			}
		};
		_mc.Stop.onPress = function ()
		{
			//停止按钮事件,因为传递了t=0,所以会从天始开始播放.
			_this._mc ["MD" + _this.i].stop ();
			_this._mc.mix.gotoAndPlay (2);
			_this._mc.Pause.gotoAndStop (2);
			this.enabled = false;
		};
		_mc.setTime.onPress=function(){
			_this.isDrag=true;
			this.startDrag(false,this.x,this.y,this.x+_this.loadW-_this.setTimeW/*this._width*/,this.y);
		}
		_mc.setTime.onRelease=_mc.setTime.onReleaseOutside=function(){
			_this.isDrag=false;
			stopDrag();
		}
		_mc.setVol.onPress=function(){
			this.startDrag(false,this.x,this.y,this.x,this.y+_this._mc.skin._height-_this.setTimeW);
		}
		_mc.setVol.onRelease=_mc.setVol.onReleaseOutside=function(){
			_this.Svolue=100-int((this._y-this.y)/_this._mc.skin._height*100);
			_this._mc["MD"+_this.i].volume =_this.Svolue;
			_this._mc.mix._yscale=_this.Svolue;
			stopDrag();
		}
	}
	//主要函数.进行相应歌曲播放器的初始化;
	private function loadMusic (num : Number)
	{
		_mc.Pause.gotoAndStop(1);
		var _this = this;
		isLoaded=false;//使其先进行加载;
		i += num;
		if (i >= myxml.getLen ())
		{
			i = 0;
		}
		if (i < 0)
		{
			i = myxml.getLen () - 1;
		}
		var Url : String = myxml.Url (i);
		var mp3Movie:String=myxml.getMMovie();
		var removeNum : Number = int (MusicArray.pop ());
		removeMediaPlay (removeNum)
		_mc.attachMovie ("MediaDisplay", "MD" + i, i);
		MusicArray.push (i);
		var type : String = Url.substr (Url.lastIndexOf (".") + 1, 3).toUpperCase ()
		PlayerVisible (type,mp3Movie);
		with (_mc ["MD" + i])
		{
			//设置播放器属性;
			autoSize = false;
			border = true;
			_width = 168;
			_height = 112;
			_x = _this._mc.skin._x - _width / 2;
			_y = _this._mc.skin._y - _height / 2 - 10;
			setMedia (Url, type);
			volume=_this.Svolue;
			addEventListener ("complete", _this);
			setInterval(_this,"loadingMusic", 250);
			_this._mc.mix.gotoAndStop (1);
		}
	}
	///完成事件;加载下一首音乐;
	private function complete ()
	{
		loadMusic (1);
	}
	///删除不需要的播放器;
	private function removeMediaPlay (num : Number)
	{
		_mc ["MD" + num].stop ();
		_mc ["MD" + num].removeMovieClip ();
	}
	///判断类型.进行相应的加载;
	private function PlayerVisible (type : String,mp3Movie:String)
	{
		if (type == "MP3")
		{
			_mc.Mp3Map.loadMovie(mp3Movie);
			_mc ["MD" + i]._visible = false;
		} else
		{
			unloadMovie (_mc.Mp3Map);
			_mc ["MD" + i]._visible = true;
		}
	}
	///进行加载运算和调整进度运算;
	private function loadingMusic(){
		var _this=this;
		//Shared.data.ID = _this.i;
		//Shared.data.Duration = _this._mc["MD"+_this.i].playheadTime;
		if (!isDrag) {
			var Played = _this._mc["MD"+_this.i].playheadTime;
			var Total  = _this._mc["MD"+_this.i].totalTime;
			var done = int(Played/Total*(_this.loadW-_this.setTimeW/*_mc.setTime._width*/));
			_mc.setTime._x = _mc.setTime.x+done;
			var time1 = int(Played/60)< 10 ? "0" + int(Played/60) : int(Played / 60);
	    	var time2 = int(Played % 60) < 10 ? "0" + int(Played% 60) : int(Played% 60);
			var time3 = int(Total / 60) < 10 ? "0" + int(Total / 60) : int(Total / 60);
			var time4 = int(Total% 60) < 10 ? "0" + int(Total % 60) : int(Total% 60);
			var Ttext=time1 + ":" + time2 + " - " + time3 + ":" + time4+newline+myxml.Title (_this.i)
			_mc.setTime.doTime.text =Ttext ;
		} else {
			var Dgl= _mc.setTime._x-_mc.setTime.x;
			_mc["MD"+i].play(Dgl/(_this.loadW-_this.setTimeW/*_mc.setTime._width*/)*(_mc["MD"+i].totalTime));
		}//是否正在调节播放进度;
		if(!isLoaded){
			var a:Number=_mc["MD" + _this.i].bytesLoaded;
			var b:Number=_mc["MD" + _this.i].bytesTotal;
			var c:Number= int(a/b*100);
			_this._mc.Mloading.gotoAndStop(c);
			if(c>99){
				isLoaded=true;
			}
		}//isLoaded的完成;
	}
}